package com.itheima.reggie.builder;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.CustomerException;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.service.AddressBookService;
import com.itheima.reggie.service.ShoppingCartService;
import com.itheima.reggie.service.UserService;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

public class FullOrderDataBuilder {
    private UserService userService;
    private FullOrderData fullOrderData;
    private ShoppingCartService shoppingCartService;
    private Orders orders;
    private AtomicInteger amount = new AtomicInteger(0);
    private AddressBookService addressBookService;
    public FullOrderDataBuilder(UserService userService, ShoppingCartService shoppingCartService, Orders orders,AddressBookService addressBookService){
        this.userService=userService;
        this.shoppingCartService=shoppingCartService;
        this.orders=orders;
        this.addressBookService=addressBookService;
        this.fullOrderData=new FullOrderData();
    }
    public FullOrderDataBuilder builderOrderDetail(){
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,orders.getUserId());
        List<ShoppingCart> shoppingCarts = shoppingCartService.list(wrapper);
        List<OrderDetail> orderDetails = shoppingCarts.stream().map((item) -> {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(orders.getId());
            orderDetail.setNumber(item.getNumber());
            orderDetail.setDishFlavor(item.getDishFlavor());
            orderDetail.setDishId(item.getDishId());
            orderDetail.setSetmealId(item.getSetmealId());
            orderDetail.setName(item.getName());
            orderDetail.setImage(item.getImage());
            orderDetail.setAmount(item.getAmount());
            amount.addAndGet(item.getAmount().multiply(new BigDecimal(item.getNumber())).intValue());
            return orderDetail;
        }).collect(Collectors.toList());

        fullOrderData.setOrderDetailList(orderDetails);
        return this;
    }
    public FullOrderDataBuilder builderOrder(){
        //查询地址数据
        Long addressBookId = orders.getAddressBookId();
        AddressBook addressBook = addressBookService.getById(addressBookId);
        if(addressBook == null){
            throw new CustomerException("用户地址信息有误，不能下单");
        }

        //查询用户数据
        User user = userService.getById(orders.getUserId());

        orders.setId(orders.getId());
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setStatus(2);
        orders.setAmount(new BigDecimal(amount.get()));//总金额
        orders.setUserId(orders.getUserId());
        orders.setNumber(String.valueOf(orders.getId()));
        orders.setUserName(user.getName());
        orders.setRemark(orders.getRemark());
        orders.setConsignee(addressBook.getConsignee());
        orders.setPhone(addressBook.getPhone());
        orders.setAddress((addressBook.getProvinceName() == null ? "" : addressBook.getProvinceName())
                + (addressBook.getCityName() == null ? "" : addressBook.getCityName())
                + (addressBook.getDistrictName() == null ? "" : addressBook.getDistrictName())
                + (addressBook.getDetail() == null ? "" : addressBook.getDetail()));
        //向订单表插入数据，一条数据
        fullOrderData.setOrders(orders);
        return this;

    }
    public FullOrderData builder(){
        return fullOrderData;
    }
}
